package com.yuexunit.finance.member.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.member.entity.MonthEarningDetailEntity;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
*@version 创建时间：2017年12月21日 下午4:33:50
*@author wenwenlong
*类说明
*/

@Implements(MonthEarningDetailService.class)
public class MonthEarningDetailServiceImpl implements MonthEarningDetailService {

	@Override
	@Transactional
	public Long createMonthEarningDetail(MonthEarningDetailEntity entity) {
		entity.setEnabled(Boolean.TRUE);
		entity.setCreateDt(DateUtil.now());
		entity.setUpdateDt(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getMonthEarningDetailId();
	}

	@Override
	public PageList<MonthEarningDetailEntity> inquiryMonthEarningDetailList(
			PageCriteria page, Integer year, Integer month,
			String financeNumber, Integer productType, Long enterpriseId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MonthEarningDetailEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (year != null && year > 0) {
			criteria.add(Restrictions.eq("year", year));
		}
		if (month != null && month > 0) {
			criteria.add(Restrictions.eq("month", month));
		}
		if (StringUtil.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (productType != null && productType > 0) {
			criteria.add(Restrictions.eq("productType", ProductTypeEnum.valueOf(productType)));
		}
		if (enterpriseId != null && enterpriseId > 0) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		return PersistUtil.get(criteria, page);
	}
	
	@Override
	public List<MonthEarningDetailEntity> inquiryMonthEarningDetailList(Integer year, Integer month,
			String financeNumber, Integer productType, Long enterpriseId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MonthEarningDetailEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (year != null && year > 0) {
			criteria.add(Restrictions.eq("year", year));
		}
		if (month != null && month > 0) {
			criteria.add(Restrictions.eq("month", month));
		}
		if (StringUtil.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (productType != null && productType > 0) {
			criteria.add(Restrictions.eq("productType", ProductTypeEnum.valueOf(productType)));
		}
		if (enterpriseId != null && enterpriseId > 0) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		return PersistUtil.get(criteria);
	}

	@Override
	@Transactional
	public void disableMonthEarning(Long monthEarningDetailId) {
		MonthEarningDetailEntity entity = PersistUtil.get(MonthEarningDetailEntity.class, monthEarningDetailId);
		entity.setEnabled(Boolean.FALSE);
		PersistUtil.update(entity);
	}
	
	@Override
	public MonthEarningDetailEntity inquiryMonthEarningDetailFinance(
			Long monthEarningDetailId) {
		return PersistUtil.get(MonthEarningDetailEntity.class, monthEarningDetailId);
	}

	@Override
	public List<MonthEarningDetailEntity> inquiryMonthEarningList(Integer year, Integer month, String financeNumber,
			Long enterpriseId, String enterpriseName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MonthEarningDetailEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (year != null && year > 0) {
			criteria.add(Restrictions.eq("year", year));
		}
		if (month != null && month > 0) {
			criteria.add(Restrictions.eq("month", month));
		}
		if (StringUtil.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (StringUtil.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.eq("enterpriseName", financeNumber));
		}
		if (enterpriseId != null && enterpriseId > 0) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		return PersistUtil.get(criteria);
	}
}
